Test Series - Data Structure

Test Number 43/115

Q: What will be the time complexity of the following code?

#include  
using namespace std; 
void func(int arr[], int left, int right) 
{ 
    	while (left < right) 
	{ 
		int temp = arr[left]; 
		arr[left] = arr[right]; 
		arr[right] = temp; 
		left++; 
		right--; 
	} 
 
}	 
 
void printArray(int arr[], int size) 
{ 
    for (int i = 0; i < size; i++) 
    cout << arr[i] << " "; 
} 
 
int main() 
{ 
	int arr[] = {1,4,3,5}; 
	int n = sizeof(arr) / sizeof(arr[0]); 
	func(arr, 0, n-1); 
	printArray(arr, n); 
	return 0; 
}
A. O(n)
B. O(log n)
C. O(1)
D. O(n log n)
Solution: The given code reverses the input array and then prints the resulting array. So the time complexity of the given code will linearly vary with the number of elements in the array and thus the time complexity will be O(n).
Q: What will be the auxiliary space requirement of the following code?

#include  
using namespace std; 
void func(int arr[], int left, int right) 
{     
	while (left < right) 
	{ 
		int temp = arr[left]; 
		arr[left] = arr[right]; 
		arr[right] = temp; 
		left++; 
		right--; 
	} 
 
}	
 
void printArray(int arr[], int size) 
{ 
    for (int i = 0; i < size; i++) 
    cout << arr[i] << " "; 
} 
 
int main() 
{ 
	int arr[] = {1,4,3,5}; 
	int n = sizeof(arr) / sizeof(arr[0]); 
	func(arr, 0, n-1); 
	printArray(arr, n); 
	return 0; 
}
A. O(1)
B. O(n)
C. O(log n)
D. O(n log n)
Solution: The given code reverses the input array and then prints the resulting array. The given code does not use any extra array to complete this task thus the auxiliary space requirement is O(1).
Q: What will be the output of the following code ?

#include  
using namespace std; 
 
void func(int arr[], int left, int right) 
{ 
    if (left >= right) 
    return; 
 
    int temp = arr[left];  
    arr[left] = arr[right]; 
    arr[right] = temp; 
 
    func(arr, left + 1, right - 1);  
}      
 
void printArray(int arr[], int size) 
{ 
    for (int i = 0; i < size; i++) 
    cout << arr[i] << " "; 
} 
 
int main() 
{ 
	int arr[] = {1,2,3,4}; 
	int n = sizeof(arr) / sizeof(arr[0]); 
	func(arr, 0, n-1); 
	printArray(arr, n); 
	return 0; 
}
A. 1 2 3 4
B. 4 3 2 1
C. 1 4 2 3
D. 4 1 2 3
Solution: The given code reverses the original array and prints the resulting array. Recursive function is used to reverse the array.
Q: What will be the time complexity of the following code?

#include  
using namespace std; 
void func(int arr[], int left, int right) 
{ 
    if (left >= right) 
    return; 
 
    int temp = arr[left];  
    arr[left] = arr[right]; 
    arr[right] = temp; 
 
    func(arr, left + 1, right - 1);  
}      
 
void printArray(int arr[], int size) 
{ 
    for (int i = 0; i < size; i++) 
    cout << arr[i] << " "; 
} 
 
int main() 
{ 
	int arr[] = {1,2,3,4}; 
	int n = sizeof(arr) / sizeof(arr[0]); 
	func(arr, 0, n-1); 
	printArray(arr, n); 
	return 0; 
}
A. O(1)
B. O(n)
C. O(log n)
D. O(n log n)
Solution: The given code reverses the original array and prints the resulting array. The number of swaps is proportional to the number of elements in the array so it requires a time complexity of O(n).
Q: What will be the output of the following code?

#include  
using namespace std; 
void func(int a[], int n, int k) 
{ 
	if (k <= n) 
	{ 
		for (int i = 0; i < k/2; i++) 
		swap(a[i], a[k-i-1]); 
	} 
 
} 
int main() 
{ 
	int a[] = {1, 2, 3, 4, 5}; 
	int n = sizeof(a) / sizeof(int), k = 3; 
	func(a, n, k); 
	for (int i = 0; i < n; ++i) 
		cout << a[i]<<" ";
	return 0; 
}
A. 3 2 1 4 5
B. 5 4 3 2 1
C. 1 2 5 4 3
D. error
Solution: The given code reverses only a specified segment of the input array. As the value of k is given to be 3 in the code thus only the first three elements of the array will be reversed.
Q: What will be the time complexity of the following code?

#include  
using namespace std; 
 
void func(int a[], int n, int k) 
{ 
	if (k <= n) 
	{ 
		for (int i = 0; i < k/2; i++) 
		swap(a[i], a[k-i-1]); 
	} 
 
} 
int main() 
{ 
	int a[] = {1, 2, 3, 4, 5}; 
	int n = sizeof(a) / sizeof(int), k = 3; 
	func(a, n, k); 
	for (int i = 0; i < n; ++i) 
		cout << a[i]<<" ";
	return 0; 
}
A. O(k)
B. O(n)
C. O(k log k)
D. O(n log n)
Solution: The given code reverses only a specified segment of the input array. This segment is decided by the value of k so the time complexity of the code will be O(k).
Q: When array reversal and rotation is applied to the same array then the output produced will also be the same every time.
A. true
B. false
C. ...
D. ...
Solution: Array rotation and array reversal are different operations and thus they give different outputs when applied to the same array.
Q: Which of the following is the predefined function for array reversal in C++ ?
A. reverse()
B. arr_reverse()
C. array_reverse()
D. rev()
Solution: The predefined function for reversing an array is reverse() in C++. It is defined under the library algorithm and requires 2 arguments.
Q: Which of the following is the predefined function for array reversal in javascript?
A. reverse()
B. arr_reverse()
C. array_reverse()
D. rev()
Solution: The predefined function for reversing an array is reverse() in javascript. It does not requires any argument.
Q: Predefined function reverse() in C++ is available under which header file?
A. math
B. stdio
C. stdlib
D. algorithm
Solution: The predefined function for reversing an array is reverse() in C++ which comes under the library called an algorithm. It requires 2 arguments the first being the pointer to the starting index of the array and the second being the pointer to the last index of the array.

You Have Score    /10